\section{Serialiser}
\label{source_code:serializer}

\subsection{serializer.hpp}
\begin{lstlisting}[language=C++,numbers=left, numberstyle=\tiny\color{gray}, stepnumber=2, numbersep=5pt, backgroundcolor=\color{white}, morekeywords={*,uint8_t,uint16_t,uint32_t,uint64_t}]
#ifndef SERIALIZER_HPP
#define SERIALIZER_HPP

#include <stdlib.h>
#include <vector>
#include <assert.h>
#include <boost/signals2.hpp>

#include <stdlib.h>

class serializer
{
public:
    std::vector<uint8_t> data;

    boost::signals2::signal<void (uint8_t*, uint32_t)> signal_new_buffer;

    serializer(uint32_t size = 0);
    ~serializer();

    uint32_t feed(uint8_t* ptr, uint32_t size);
    std::vector<uint8_t>& serialize();
    void deserialize_signal(std::vector<uint8_t>& data);
    void deserialize_signal(uint8_t* data_ptr, uint32_t size);
    void reset(uint32_t capacity);
    uint32_t size();
};

#endif
\end{lstlisting}

\subsection{serializer.cpp}
\begin{lstlisting}[language=C++,numbers=left, numberstyle=\tiny\color{gray}, stepnumber=2, numbersep=5pt, backgroundcolor=\color{white}, morekeywords={*,uint8_t,uint16_t,uint32_t,uint64_t}]
#include "serializer.hpp"

serializer::serializer(uint32_t size)
{
    if (size) data.reserve(size);
}

serializer::~serializer()
{
    // Empty destructor
}

uint32_t serializer::size()
{
    return data.size();
}

// Feed a buffer to serializer. Serializer will append a COPY of the buffer to end of the vector, and returns an index to the vector where the buffer ENDS.
uint32_t serializer::feed(uint8_t *ptr, uint32_t size)
{
    uint8_t size_array[sizeof(uint32_t)];
    memcpy(&size_array, &size, sizeof(uint32_t));

    uint32_t index = data.size();
    data.insert(data.end(), size_array, size_array+sizeof(uint32_t));

    data.insert(data.end(), ptr, ptr+size);

    return index;
}

std::vector<uint8_t>& serializer::serialize()
{
    std::vector<uint8_t>& data_ref = data;
    return data_ref;
}

// deserialize function emits a signal with ptr to buffer, and length of respective buffer.
void serializer::deserialize_signal(std::vector<uint8_t>& serialized_data)
{
    uint32_t next_buffer_size;
    uint32_t index = 0;
    while(index < serialized_data.size()-1)
    {
        memcpy(&next_buffer_size, &serialized_data[index],sizeof(uint32_t));
        // If next buffer is longer than remaining data in vector, an error has occured: break from loop.

        if (next_buffer_size > serialized_data.size()-index || next_buffer_size == 0) break;
        index += sizeof(uint32_t);
        signal_new_buffer(&serialized_data[index], next_buffer_size);
        index += next_buffer_size;
    }
}

void serializer::deserialize_signal(uint8_t* data_ptr, uint32_t size)
{
    uint32_t next_buffer_size;
    uint32_t index = 0;
    while(index < size-1)
    {
        memcpy(&next_buffer_size, data_ptr+index,sizeof(uint32_t));
        if (next_buffer_size > size-index || next_buffer_size == 0) break;
        index += sizeof(uint32_t);
        signal_new_buffer(data_ptr+index, next_buffer_size);
        index += next_buffer_size;
    }
}

void serializer::reset(uint32_t capacity)
{
    data.clear();
    if (capacity) data.reserve(capacity);
}
\end{lstlisting}